﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TileEngine.tiles;
using TileEngine.tiles.material;

namespace TileEngine
{
    class WorldCreator
    {
        public static void Create(Map tiles) {
            int r=100;
            int baseR=100;
            for (int i = 0; i < tiles.MapWidth; i++)
            {//yüzey
                if (i > tiles.MapWidth - 100)
                    r += Math.Sign(baseR - r) * Global.myRandom.Next() % 2 + Global.myRandom.Next() % 3 - 1;
                else
                    r += Global.myRandom.Next() % 3 - 1;
                for (int j = 0; j < r; j++)
                {
                    tiles[i, j].tile = new Empty(i, j);
                    tiles[i, j].wall = null;
                }
                tiles[i, r].tile = new Grass(i, r);
            }

            for (int i = 0; i < tiles.MapHeight; i++)
            {
                for (int j = 0; j < tiles.MapWidth/200; j++)
                {
                    createRandomShaped<Empty>(tiles,Global.myRandom.Next() % 21 + 2, i, Global.myRandom.Next() % tiles.MapWidth);
                }
            }
            for (int i = 0; i < 50; i++) {
                createRandomShaped<Empty>(tiles,Global.myRandom.Next() % 150 + 1, Global.myRandom.Next() % tiles.MapHeight, Global.myRandom.Next() % tiles.MapWidth);
            }
            //int top = getTopof(100);
            //createRandomShaped<Empty>(tiles, 500,top,100);

            for (int i = 0; i < 1050; i++)
                createRandomShaped<Copper>(tiles, Global.myRandom.Next() % 15 + 1, Global.myRandom.Next() % tiles.MapHeight, Global.myRandom.Next() % tiles.MapWidth);
            for (int i = 0; i < 1000; i++)
                createRandomShaped<Iron>(tiles, Global.myRandom.Next() % 15 + 1, Global.myRandom.Next() % tiles.MapHeight, Global.myRandom.Next() % tiles.MapWidth);
            for (int i = 0; i < 1000; i++)
                createRandomShaped<Silver>(tiles, Global.myRandom.Next() % 15 + 1, Global.myRandom.Next() % tiles.MapHeight, Global.myRandom.Next() % tiles.MapWidth);
            for (int i = 0; i < 1000; i++)
                createRandomShaped<Gold>(tiles, Global.myRandom.Next() % 15 + 1, Global.myRandom.Next() % tiles.MapHeight, Global.myRandom.Next() % tiles.MapWidth);
            for (int i = 0; i < 1000; i++)
                createRandomShaped<Coal>(tiles, Global.myRandom.Next() % 15 + 1, Global.myRandom.Next() % tiles.MapHeight, Global.myRandom.Next() % tiles.MapWidth);
            for (int i = 0; i < 1000; i++)
                createRandomShaped<Phosphor>(tiles, Global.myRandom.Next() % 15 + 1, Global.myRandom.Next() % tiles.MapHeight, Global.myRandom.Next() % tiles.MapWidth);
            for (int i = 0; i < 1000; i++)
                createRandomShaped<Boron>(tiles, Global.myRandom.Next() % 15 + 1, Global.myRandom.Next() % tiles.MapHeight, Global.myRandom.Next() % tiles.MapWidth);
            for (int i = 0; i < 1000; i++)
                createRandomShaped<Crome>(tiles, Global.myRandom.Next() % 15 + 1, Global.myRandom.Next() % tiles.MapHeight, Global.myRandom.Next() % tiles.MapWidth);
            for (int i = 0; i < 1000; i++)
                createRandomShaped<Nikel>(tiles, Global.myRandom.Next() % 15 + 1, Global.myRandom.Next() % tiles.MapHeight, Global.myRandom.Next() % tiles.MapWidth);
            for (int i = 0; i < 1000; i++)
                createRandomShaped<Cobalt>(tiles, Global.myRandom.Next() % 15 + 1, Global.myRandom.Next() % tiles.MapHeight, Global.myRandom.Next() % tiles.MapWidth);
            for (int i = 0; i < 1000; i++)
                createRandomShaped<Platinum>(tiles, Global.myRandom.Next() % 15 + 1, Global.myRandom.Next() % tiles.MapHeight, Global.myRandom.Next() % tiles.MapWidth);
            for (int i = 0; i < 1000; i++)
                createRandomShaped<Titanium>(tiles, Global.myRandom.Next() % 15 + 1, Global.myRandom.Next() % tiles.MapHeight, Global.myRandom.Next() % tiles.MapWidth);
            for (int i = 0; i < 1000; i++)
                createRandomShaped<Mithril>(tiles, Global.myRandom.Next() % 15 + 1, Global.myRandom.Next() % tiles.MapHeight, Global.myRandom.Next() % tiles.MapWidth);
            for (int i = 0; i < 1000; i++)
                createRandomShaped<Adamentine>(tiles, Global.myRandom.Next() % 15 + 1, Global.myRandom.Next() % tiles.MapHeight, Global.myRandom.Next() % tiles.MapWidth);

            for (int i = 0; i < Global.currentWorld.tiles.MapWidth; i+=5+Global.myRandom.Next()%20)
            {
                int w = getTopof(i);
                for (int j = w-5-Global.myRandom.Next()%20; j < w; j++)
                {
                    Global.currentWorld.tiles[i, j].tile = new Tree(i, j);
                }
            }
        }
        static int getTopof(int x)
        {
            for (int i = 0; i < Global.currentWorld.tiles.MapHeight; i++)
            {
                if(Global.currentWorld.tiles[x,i].tile.GetType()!=typeof(Empty))
                    return i;
            }
            return 0;
        }
        static void createRandomShaped<T>(Map tiles, int depth, int y, int x) where T : Itile, new()
        {
            if (depth < 1)
                return;
            if (tiles[x, y].tile.GetType() != typeof(T) && tiles[x, y].tile.GetType() != typeof(Empty))
            {
                tiles[x, y].tile = new T();
                tiles[x,y].tile.setCoord(x,y);
            }
            if (tiles[x, y + 1].tile.GetType() != typeof(T) && tiles[x, y + 1].tile.GetType() != typeof(Empty))
                createRandomShaped<T>(tiles, Global.myRandom.Next() % 4 - 5 + depth, y + 1, x);
            if (tiles[x, y - 1].tile.GetType() != typeof(T) && tiles[x, y - 1].tile.GetType() != typeof(Empty))
                createRandomShaped<T>(tiles, Global.myRandom.Next() % 4 - 5 + depth, y - 1, x);
            if (tiles[x + 1, y].tile.GetType() != typeof(T) && tiles[x + 1, y].tile.GetType() != typeof(Empty))
                createRandomShaped<T>(tiles, Global.myRandom.Next() % 4 - 5 + depth, y, x + 1);
            if (tiles[x-1,y].tile.GetType() != typeof(T) && tiles[x-1,y].tile.GetType() != typeof(Empty))
                createRandomShaped<T>(tiles, Global.myRandom.Next() % 4 - 5 + depth, y, x - 1);
            
        }
    }
}
